<?php
class View
{
	public static $vars	=	array();	//全局共享的模板变量
	public static $layout;		//页面布局
	
	public static $cacheDir;	//缓存存储位置
	
	static function assign($data)
	{
		if(is_array($data)){
			self::$vars	=	array_merge(self::$vars, $data);
		}
		else{
			self::$vars[$data]	=	func_get_arg(1);
		}
	}

	static function fetch($_act, $_layout=null)
	{
		//自定义变量
		View::assign(array(
			'root'	=>	WEB_ROOT,
			'theme'	=>	WEB_THEME,
			'skin'	=>	WEB_THEME.'/skin',
		));
		
		self::loadEngine();
		if(is_callable(array($GLOBALS['view_engine'], 'fetch')))
		{
			call_user_func(array($GLOBALS['view_engine'], 'fetch'), View::tpl($_act));
		}
		else
		{
			ob_flush();
			self::display($_act, $_layout);
			return ob_get_clean();
		}
	}

	static function display($_act, $_layout=null)
	{
		//自定义变量
		View::assign(array(
			'root'	=>	WEB_ROOT,
			'theme'	=>	WEB_THEME,
			'skin'	=>	WEB_THEME.'/skin',
		));

		self::loadEngine();
		$func	=	array($GLOBALS['view_engine'], 'display');
		$tplf	=	View::tpl($_act);
		if(!$tplf) {
			$s	= $_act?$_act:$_SERVER['REQUEST_URI'];
			Show::error("Can`t find tpl file: {$s} ");
		}

		if(false===$_layout) {
			if(MOD_DEBUG) {
				try {
					call_user_func($func, $tplf);
				}
				catch(Exception $_){
					Show::error($_);
				}
			}
			else {
				call_user_func($func, $tplf);
			}
		}
		else {
			if(!$_layout){
				$_layout = (View::$layout ? View::$layout : Core::$convent['layout']);
			}

			if($_layout) {
				$_layfile	=	my_path('%/FASHION/'.$_layout.Core::$convent['t_suffix']);
				if(!$_layfile) {
					Show::error("Can`t find layout: '{$_layout}'");
				}

				View::assign('tpl_file', $tplf);

				if(MOD_DEBUG) {
					try {
						call_user_func($func, $_layfile);
					}
					catch(Exception $_){
						Show::error($_);
					}
				}
				else {
					call_user_func($func, $_layfile);
				}
			}
			else {
				if(MOD_DEBUG) {
					try {
						call_user_func($func, $tplf);
					}
					catch(Exception $_){
						Show::error($_);
					}
				}
				else {
					call_user_func($func, $tplf);
				}
			}
		}
	}
	
	static function clearCache($theme=null)
	{
		self::loadEngine();
		if(is_callable(array($GLOBALS['view_engine'], 'clearCache')))
		{
			call_user_func_array(array($GLOBALS['view_engine'], 'clearCache'), array($theme));
		}
	}

	/**
	 * 设置页面布局
	 */
	static function setLayout($layout)
	{
		self::$layout	=	$layout;
	}
	/**
	 * 设置Content-type，如 "text/html","text/vnd.wml"
	 */
	static function setContentType($type)
	{
		header('Content-type: '.$type.'; charset=utf-8');
	}
	/** 设置或取到当前系统使用的主题  */
	static function theme($theme=null)
	{
		static $t=null;
		if($theme!==null) {
			return $t = $theme;
		}
		return $t!==null?$t:Core::$convent['theme'];
	}
	
	/**
	 * 通过action取到模板文件名
	 */
	static function tpl($act=null)
	{
		if(!$act) {
			$act	=	$_REQUEST['a'];
		}
		else {
			$act	=	strtolower($act);
			false===strpos($act,'-')?$act.='-show':null;
		}
		
		return my_path( '%/'.strtok(strtolower($act), '-')
						.'/'.str_replace('-', '_', strtok(''))
						.Core::$convent['t_suffix'] );
	}

	/**
	 * 加载模板处理引擎
	 */
	static function loadEngine()
	{
		static $_init;
		if(!$_init)
		{
			$_init	=	1;
			$ve	=	Core::cfg('view_engine');
			import("#/include/{$ve}.view.php");

			self::$cacheDir	=	DATA_PATH."/cache";

			$GLOBALS['view_engine']	=	ucfirst($ve).'View';
			call_user_func(array($GLOBALS['view_engine'], 'initialize'));
		}
	}
	
}
?>
